﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Job.Web.Framework;
using Job.Logic.Personal;
using Job.Entity;

namespace Job.Web.userControls.Personal
{
    public partial class TrainList : BaseCtrl
    {
        int personalId = myR["resumeid"].IntValue;
        int tranid = myR["tranid"].IntValue;
        int subjectid = myR["subjectid"].IntValue;

        PersonalLogic pl = new PersonalLogic();

        private void ShowMessage(string message)
        {
            this.message.InnerHtml = "<font color=red>" + message + "</font>";
            this.message.Visible = true;
            this.message.Height = "30px";
            this.message.VAlign = "middle";
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.InitControls();
            if (!Page.IsPostBack)
            {
                LoadData();
            }
        }

        private void InitControls()
        {
            tbBeginDate.Attributes.Add("onfocus", "calendar()");
            tbEndDate.Attributes.Add("onfocus", "calendar()");
        }

        private void LoadData()
        {
            try
            {
                gvTran.DataSource = pl.GetTransByPersonalId(personalId);
                gvTran.DataBind();

                JobPersonTran tran = pl.GetSingleTran(tranid);
                if (tran != null)
                {
                    lbledu.Text = "编辑培训经历";
                    txtBook.Text = tran.TranCertify;
                    txtTranName.Text = tran.TranName;
                    txtDesc.Text = tran.TranDesc;
                    if (tran.StartTime.HasValue)
                    {
                        tbBeginDate.Text = tran.StartTime.Value.ToString("yyyy-MM-dd");
                    }
                    if (tran.EndTime.HasValue)
                    {
                        tbEndDate.Text = tran.EndTime.Value.ToString("yyyy-MM-dd");

                    }
                }
                else
                {
                    lbledu.Text = "新增培训经历";
                }
            }
            catch (Exception ex)
            {
                Logger.WriteLog("培训经历查询失败:\r\n" + ex.Message);
                ShowMessage("查询失败:\r\n" + ex.Message);

            }
        }

        protected void gvTran_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            if (e.CommandName.ToLower() == "edit")
            {
                //edit
                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this.personalId + "&tranid=" + id;
                Response.Redirect(url);

            }
            else if (e.CommandName.ToLower() == "delete")
            {
                JobPersonTran _tran = pl.GetSingleTran(id);
                pl.DeleteTran(_tran);

                string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this.personalId;
                Response.Redirect(url);
            }
        }

        protected void gvTran_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                JobPersonTran tran = null;
                if (tranid > 0)
                {
                    tran = pl.GetSingleTran(tranid);
                }
                else
                {
                    tran = new JobPersonTran();
                }
                tran.TranName = txtTranName.Text.Trim();
                tran.TranDesc = txtDesc.Text.Trim();
                tran.TranCertify = txtBook.Text.Trim();
                string defaultTime = "1901-01-01";
                DateTime beginTime = Convert.ToDateTime(defaultTime);
                DateTime endTime = Convert.ToDateTime(defaultTime);
                DateTime.TryParse(tbBeginDate.Text.Trim(), out beginTime);
                DateTime.TryParse(tbEndDate.Text.Trim(), out endTime);
                if (!beginTime.ToString("yyyy-MM-dd").Equals(defaultTime))
                {
                    tran.StartTime = beginTime;
                }
                if (!endTime.ToString("yyyy-MM-dd").Equals(defaultTime))
                {
                    tran.EndTime = endTime;
                }
                tran.PersonInfoId = personalId;
                pl.ActionTran(tran);
            }
            catch (Exception ex)
            {
                Logger.WriteLog("培训经历保存失败:\r\n" + ex.Message);
                ShowMessage("保存失败:\r\n" + ex.Message);
                return;
            }
            string url = WebCommon.GetApplicationPath(Request) + "Person/PersonalResume.aspx?categoryid=1&subjectid=" + this.subjectid + "&resumeid=" + this.personalId;
            Response.Redirect(url);
        }
    }
}